package leetcode;

import java.util.Scanner;

public class code92 {

    /**
     * 给你单链表的头指针 head 和两个整数 left 和 right ，其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。
     * <p>
     * <p>
     * 示例 1：
     * <p>
     * <p>
     * 输入：head = [1,2,3,4,5], left = 2, right = 4
     * 输出：[1,4,3,2,5]
     * 示例 2：
     * <p>
     * 输入：head = [5], left = 1, right = 1
     * 输出：[5]
     */

    //创建一个列表
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);



        String[] split = scanner.nextLine().split(" ");
        int[] arr = new int[split.length];
        //输入一个left和right
        int left = Integer.parseInt(split[0]);
        int right = Integer.parseInt(split[1]);

        for (int i = 2; i < arr.length; i++) {
            arr[i] = Integer.parseInt(split[i]);
        }

        //数组 封装成链表
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        for (int i = 0; i < arr.length; i++) {
            ListNode node = new ListNode(arr[i]);
            cur.next = node;
            cur = cur.next;
        }

        ListNode listNode1 = reverseBetween(dummy.next, left, right);

        //输出链表
        while (listNode1 != null) {
            System.out.println(listNode1.val);
            listNode1 = listNode1.next;
        }
    }

    public static ListNode reverseBetween(ListNode head, int left, int right) {

        ListNode pre = head;
        //找到第left个节点
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }

        //找到第right个节点
        ListNode current = pre;
        for (int i = 0; i < right - left + 1; i++) {
            current = current.next;
        }

        System.out.println(pre.val);
        System.out.println(current.val);


        return pre;
    }


}
